<template>
  <div>
    <template>
      <div>
        <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
          <el-form-item prop="name" label="部门名称">
            <el-input v-model="form.name" style="width:90%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item prop="code" label="部门编码">
            <el-input v-model="form.code" style="width:90%" placeholder="1-50个字符" />
          </el-form-item>
          <el-form-item prop="manager" label="部门负责人">
            <el-select v-model="form.manager" style="width:90%" placeholder="请选择">
              <el-option v-for="item in employees" :key="item.id" :value="item.username" :label="item.username" />
            </el-select>
          </el-form-item>
          <el-form-item prop="introduce" label="部门介绍">
            <el-input v-model="form.introduce" style="width:90%" placeholder="1-300个字符" type="textarea" :rows="3" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
            <el-button size="small" @click="hCancel">取消</el-button>
          </el-form-item>
        </el-form>
      </div>
    </template>
  </div>
</template>

<script>
// 接口
import { getEmployeeSimpleAPI, addDepartmentsAPI, getDepartDetailAPI, updateDepartmentsAPI } from '@/api/departments'
export default {
  name: 'DeptDialog',
  props: {
    // 当前部门id
    parentId: {
      type: [Number, String],
      required: true
    },
    // 是否是编辑
    isEdit: {
      type: Boolean,
      default: false
    },
    // 表单验证列表
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    // 自定义校验部门编码
    const validCode = (rule, value, callback) => {
      let codelist = this.originList.map(item => item.code)

      if (this.isEdit) {
        codelist = this.originList.filter(item => item.id !== this.parentId).map(item => item.code)
      }
      codelist.includes(value) ? callback(new Error(`部门编码${value}已存在`)) : callback()
    }

    // 自定义部门名称校验
    const validName = (rule, value, callback) => {
      let nameList = this.originList.filter(item => item.pid === this.parentId).map(item => item.name)

      // 如果是编辑的情况
      if (this.isEdit) {
        // 找到分类的父级元素
        const dept = this.originList.find(item => item.id === this.parentId)
        // 取出父级的 id
        const pid = dept.pid
        // 过滤，筛选出同一级别的数据
        nameList = this.originList
          .filter(item => item.pid === pid && item.id !== this.parentId)
          .map(item => item.name)
      }
      nameList.includes(value) ? callback(new Error(`部门名称${value}已存在`)) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employees: [], // 部门负责人列表
      // 验证规则
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { validator: validName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { validator: validCode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '部门负责人不能为空', trigger: 'blur' }
        ],
        introduce: [
          { required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '部门介绍要求1-300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 调用获取员工列表方法
    this.getEmployeeSimpleFn()
    console.log(this.isEdit)
    // 调用查询部门详情方法
    this.getDepartDetailFn()
  },
  methods: {
    // 获取员工列表方法
    async getEmployeeSimpleFn() {
      const res = await getEmployeeSimpleAPI()
      this.employees = res.data
    },

    // 查询部门详情方法
    async getDepartDetailFn() {
      if (this.isEdit) {
        const res = await getDepartDetailAPI(this.parentId)
        this.form = res.data
      }
    },

    // 添加部门
    async addDept() {
      await addDepartmentsAPI({ ...this.form, pid: this.parentId })
    },

    // 编辑部门
    async editDept() {
      const res = await updateDepartmentsAPI(this.parentId, this.form)
      console.log(res)
    },

    // 添加编辑确认按钮
    hSubmit() {
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          // 判断是否是编辑状态
          this.isEdit ? this.editDept() : this.addDept()
          this.$emit('update-depart')
        }
      })
    },

    // 取消编辑确认按钮
    hCancel() {
      this.$emit('close')
    }
  }
}
</script>

<style>

</style>
